package lib.bone.admin.controller.nepu;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.access.config.cycle.NepuCycle;
import lib.bone.access.config.cycle.NepuCycleService;
import lib.bone.access.config.cycle.dto.NepuCycleAddDTO;
import lib.bone.access.config.cycle.dto.NepuCycleQueryDTO;
import lib.bone.access.config.cycle.dto.NepuCycleUpdateDTO;
import lib.bone.admin.anno.WebLog;
import lib.bone.admin.constant.MethodTypeConst;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.core.redis.RedisCache;
import lib.bone.system.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * cg
 */
@RestController
@RequestMapping("nepu")
@Api(tags = "周期管理")
public class NepuCycleController {

    @Autowired
    NepuCycleService service;

    @Autowired
    RedisCache redisCache;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @GetMapping("cycle/now")
    @ApiOperation(value = "获取当前周期信息")
    public ResponseDTO<NepuCycle> getNowCycle() {

//        Date now = DateUtil.date(); // 获取当前日期
//        Optional<NepuCycle> cycleOp = service.findNowCycle(now);
//        return cycleOp.map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND, "当前不在周期内"));
//        Optional<NepuCycle> cycleOp = service.findNowCycle("科研周期");
//        return cycleOp.map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND, "当前不在周期内"));
        return ResponseDTO.succData(service.getNowResearchCycle());
    }

    @GetMapping("cycle/previous")
    @ApiOperation(value = "获取上一周期信息")
    public ResponseDTO<NepuCycle> getPreviousCycle() {

//        Date now = DateUtil.date(); // 获取当前日期
//        Optional<NepuCycle> cycleOp = service.findNowCycle(now);
//        return cycleOp.map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND, "当前不在周期内"));
//        Optional<NepuCycle> cycleOp = service.findNowCycle("科研周期");
//        return cycleOp.map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND, "当前不在周期内"));
        return ResponseDTO.succData(service.getPreviousResearchCycle());
    }

//    @SaCheckRole("admin")
    //@WebLog(module = "nepu-access",operate = MethodTypeConst.INSERT,allowMask = 0)
    @PostMapping("cycle")
    @ApiOperation(value = "新增周期")
    public ResponseDTO saveOne(@RequestBody @Validated NepuCycleAddDTO nepuCycleAddDTO) {

        NepuCycle nepuCycle = new NepuCycle();
        BeanUtil.copyProperties(nepuCycleAddDTO, nepuCycle, CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
            return ResponseDTO.succData(service.saveOne(nepuCycle));
    }

    @GetMapping("cycle")
    @ApiOperation(value = "获取周期列表")
    public ResponseDTO<Object> getCycle(NepuCycleQueryDTO dto){
        Sort sort;
        if(dto.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, dto.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, dto.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(dto.getPageNum() - 1 , dto.getPageSize(), sort);

        NepuCycle cycle = new NepuCycle();
        BeanUtil.copyProperties(dto,cycle, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<NepuCycle> example = Example.of(cycle);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @WebLog(module = "nepu-access",operate = MethodTypeConst.DELETE)
    @SaCheckRole("admin")
    @DeleteMapping("cycle/{cycleId}")
    @ApiOperation(value = "删除周期")
    public ResponseDTO deletePost(@PathVariable("cycleId") String cycleId) {

            return service.deleteById(cycleId);

    }

    @GetMapping("cycle/{cycleId}")
    @ApiOperation(value = "查询周期")
    public ResponseDTO getPost(@PathVariable("cycleId") String cycleId) {
        Optional<NepuCycle> cycleOp = service.getById(cycleId);
        if (!cycleOp.isPresent()) {
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
        }
        return ResponseDTO.succData(cycleOp.get());
    }

    @WebLog(module = "nepu-access",operate = MethodTypeConst.UPDATE)
    @SaCheckRole("admin")
    @PutMapping("cycle")
    @ApiOperation(value = "更新周期", notes = "")
    public ResponseDTO<NepuCycle> updateOne(@RequestBody @Validated NepuCycleUpdateDTO nepuCycleUpdateDTO) {

        Optional<NepuCycle> cycleOp = service.getById(nepuCycleUpdateDTO.getId());
        if (!cycleOp.isPresent()) {
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
        }
        NepuCycle cycle = cycleOp.get();
        BeanUtil.copyProperties(nepuCycleUpdateDTO, cycle, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(service.updateOne(cycle));
    }
}

